package com.qf.hotel.controller.foodtype;

import com.qf.hotel.pojo.FoodType;
import com.qf.hotel.service.FoodTypeService;
import com.qf.hotel.service.impl.FoodTypeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/**
 * @author: GsheN丶
 * @date: 2021/6/24 19:54
 * @description:条件查询controller
 */
@WebServlet("/foodtype/search")
public class FoodTypeFindByConditionController extends HttpServlet {

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1字符编码设置
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");


        try {
            //2接收参数
            String keyword = request.getParameter("keyword");

            //3创建FoodType对象
            FoodType foodType = new FoodType();
            foodType.setTypeName(keyword);

            //4.创建业务对象
            FoodTypeService foodTypeService = new FoodTypeServiceImpl();
            List<FoodType> foodTypes = foodTypeService.findByCondition(foodType);

            //5.存储在域对象中
            request.setAttribute("foodTypes",foodTypes);

            //6.转发到显示数据的页面
            request.getRequestDispatcher("/backend/detail/foodtype/foodtype-list.jsp").forward(request,response);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
